<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>角色管理</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../../styles/common.css"/>
    <link rel="stylesheet" href="../../../styles/page.css"/>
</head>
<body>
<div class="dashboard-container" id="sysRole-app">
    <div class="container">
        <div class="tableBar">
            <el-input  v-model="conditions.input"  placeholder="请输入角色名称" style="width: 250px" clearable  @keyup.enter.native="handleQuery">
                <i slot="prefix" class="el-input__icon el-icon-search"  style="cursor: pointer"  @click="init"></i>
            </el-input>
            <div class="tableLab">
                <el-button  type="primary" class="continue" @click="addClass('class')"> + 新增角色 </el-button>
            </div>
        </div>
        <el-table  :data="pagination.tableData"  stripe  class="tableBox" >
            <el-table-column  prop="id" label="角色id"  /></el-table-column>
            <el-table-column  prop="name" label="角色名称"  /></el-table-column>
            <el-table-column  prop="remark" label="角色描述"  /></el-table-column>
            <el-table-column  prop="companyName" label="所属企业名称"  /></el-table-column>
            <el-table-column label="操作" width="160" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="small" class="blueBug" @click="editHandle(scope.row)">修改</el-button>
                    <el-button type="text" size="small" class="delBut non" @click="deleteHandle(scope.row.id)">删除</el-button>
                    <el-button type="text" size="small" class="infoBut" @click="showRoleModule(scope.row.id)">分配权限</el-button>

                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pagination.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pagination.counts"
                @size-change="handleSizeChange"
                :current-page.sync="pagination.page"
                @current-change="handleCurrentChange"></el-pagination>
    </div>
    <el-dialog :title="classData.title" :visible.sync="classData.dialogVisible" width="30%" :before-close="handleClose">
        <el-form class="demo-form-inline" label-width="100px">
            <el-form-item label="角色名称：">
                <el-input v-model="classData.dialogForm.name" placeholder="请输入角色名称"/>
            </el-form-item>

            <el-form-item label="角色描述：">
                <el-input v-model="classData.dialogForm.remark" placeholder="请输入角色描述" />
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="medium" @click="classData.dialogVisible = false">取 消</el-button>
            <el-button type="primary" size="medium" @click="submitForm()">确 定</el-button>
        </span>
    </el-dialog>

    <el-dialog :title="roleModule.title" :visible.sync="roleModule.dialogVisible" width="30%" :before-close="roleModuleClose">
        <el-form class="demo-form-inline" label-width="100px">
            <el-form-item label="权限树：">
                <el-tree
                        ref="tree"
                        :data="moduleList"
                        show-checkbox
                        node-key="id"
                        default-expand-all
                        :props="defaultProps">
                </el-tree>

            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="medium" @click="roleModule.dialogVisible = false">取 消</el-button>
            <el-button type="primary" size="medium" @click="updateRoleModule()">确 定</el-button>
        </span>
    </el-dialog>
</div>
<script src="../../../plugins/vue/vue.js"></script>
<script src="../../../plugins/element-ui/index.js"></script>
<script src="../../../plugins/axios/axios.min.js"></script>
<script src="../../../js/request.js"></script>
<script src="../../../api/sysRole.js"></script>
<script type="text/javascript">
    new Vue({
        el: '#sysRole-app',
        data() {
            return {
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                action: '',
                type: '',
                moduleList:[],
                roleModule:{
                    title:'分配权限',
                    dialogVisible:false,
                    checkModuleIds:[],
                    roleId:'',
                },
                classData: {
                    'title': '添加角色',
                    'dialogVisible': false,
                    dialogForm:{}
                },
                pagination:{
                    tableData: [],
                    page: 1,
                    pageSize: 5,
                    counts:0,
                },
                conditions:{
                    input:''
                }
            }
        },
        methods: {
            async init() {
                const params = {
                    page: this.pagination.page,//当前页码
                    pageSize: this.pagination.pageSize,//每页大小
                    name: this.conditions.input ? this.conditions.input : undefined//企业名称
                };
                await getRolePage(params).then(res => {
                    if (String(res.code) === '1') {
                        this.pagination.tableData = res.data.records;
                        this.pagination.counts = res.data.total;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            // 添加
            addClass(st) {
                this.classData.title = '新增角色';
                this.action = 'add';
                this.classData.dialogVisible = true
            },
            editHandle(dat) {
                //赋值
                this.classData.title = '修改角色';
                this.action = 'edit';
                this.classData.dialogForm = dat;
                this.classData.dialogVisible = true;//打开修改框
            },
            // 关闭弹窗
            handleClose(st) {
                this.classData.dialogVisible = false;
                this.classData.dialogForm = {};
            },
            //删除
            deleteHandle(id) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    deleteRole(id).then(res => {
                        if (res.code === 1) {
                            this.$message.success('删除成功！');
                            this.handleQuery()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            //数据提交
            submitForm(st) {
                const classData = this.classData.dialogForm;
                const valid = (classData.name === 0 || classData.name) ;
                if (this.action === 'add') {
                    if (valid) {
                            addRole(classData).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('添加成功！');
                                    if (!st) {
                                        this.classData.dialogVisible = false
                                    } else {
                                        this.classData = {};
                                    }
                                    this.handleQuery();
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                    } else {
                        this.$message.error('请输入名称')
                    }
                } else if (valid) {
                    editRole(classData).then(res => {
                        if (res.code === 1) {
                                this.$message.success('修改成功！');
                                this.classData.dialogVisible = false;
                                this.handleQuery();
                                this.classData.dialogForm = {};
                            } else {
                                this.$message.error(res.msg || '操作失败')
                            }
                        }).catch(err => {
                            this.$message.error('请求出错了：' + err)
                        })
                } else {
                    this.$message.error('请输入名称')
                }
            },
            // 全部操作
            handleSelectionChange(val) {
                let checkArr = [];
                val.forEach((n) => {
                    checkArr.push(n.id)
                });
                this.checkList = checkArr
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val;
                this.init()
            },
            //关闭分配权限弹框
            roleModuleClose(){
                this.roleModule.dialogVisible = false;
                this.$refs.tree.setCheckedKeys([]);
            },
            //初始化架子模块列表
            initModuleList(){
                findModuleList().then(res => {
                    if (String(res.code) === '1') {
                        this.moduleList = res.data;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
            },
            //打开分配权限对话框
            showRoleModule(id){
                this.roleModule.dialogVisible = true;
                //1.初始化当前角色具备的模块列表
                findRoleModuleList(id).then(res => {
                    if (String(res.code) === '1') {
                        this.roleModule.checkModuleIds = res.data;

                        this.$refs.tree.setCheckedKeys(this.roleModule.checkModuleIds);
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
                //2.给角色id赋值
                this.roleModule.roleId = id;

            },
            //更新角色模块关联
            updateRoleModule(){
                const params = {
                    roleId : this.roleModule.roleId,
                    moduleIds : this.$refs.tree.getCheckedKeys()
                };

                updateRoleModule(params).then(res => {
                    if (String(res.code) === '1') {
                        this.$message.success('分配成功！');
                        this.roleModule.dialogVisible = false;
                        this.$refs.tree.setCheckedKeys([]);
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
            }
        },
        created() {
            this.init();
            this.initModuleList();
        },
    })
</script>
</body>
</html>